<template>
  <div class="comment_continer">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="table">
        <el-table :data="commentList" style="width: 100%" v-loading="isloading">
          <el-table-column prop="title" label="标题" width="250">
          </el-table-column>

          <el-table-column prop="total_comment_count" label="评论数量">
          </el-table-column>
          <el-table-column prop="fans_comment_count" label="粉丝总数量">
          </el-table-column>
          <el-table-column prop="comment_status" label="评论状态">
            <template slot-scope="scope">
              {{ scope.row.comment_status ? "正常" : "关闭" }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
              <template slot-scope="scope">
            <el-switch
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(scope.row)"
               :disabled="scope.row.disabled"
            >
            </el-switch>
             </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="page">
        <el-pagination
        background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40,50]"
          :page-size="per_page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total_count"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getArticle } from "@/request/api.js";
import { updateCommentStatus } from "@/request/api.js";
export default {
  name: "MyComment",
  data() {
    return {
        total_count:0,
        page:1,
        per_page:10,
      current:1,
      isloading: false,
      commentList: [],
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
    };
  },
  created() {
    this.initComment();
  },

  methods: {
    initComment() {
      this.isloading = true;
      getArticle({ response_type: "comment" ,page:this.page,per_page:this.per_page})
        .then((res) => {
          console.log("评论列表", res.data);
            this.total_count = res.data.data.total_count
            res.data.data.results.forEach(item=>{
                item.disabled = false
            })

          this.commentList = res.data.data.results;
          this.isloading = false;
        })
        .catch(() => {
          this.isloading = false;
        });
    },
    changeStatus(article){
        article.disabled = true
        updateCommentStatus(article.id.toString(),article.comment_status).then(()=>{
            
            
            article.disabled = false
            this.$message({
            type: 'warning',
            message:  article.comment_status ?  '开启该评论！' : '关闭该评论！'
          });
        }).catch(()=>{
            article.disabled = false
        })
    },
    handleSizeChange(val){
        console.log("页面的数量大小",val)
        this.per_page = val
        this.page = 1
        this.initComment()
    },
    handleCurrentChange(val){
        this.page = val
        this.initComment()
    }
  },
};
</script>

<style lang="less" scoped>
.page {
  text-align: center;
  margin-top: 35px;
}
</style>
